package caseManagerBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.HttpResponse;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.Http;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2018/12/20.
 * 运行ui测试用例
 *
 */
public class RunCase implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        JSONObject executeScript = new JSONObject();
        JSONArray caseScript;
        String caseName;
        String describe;
        String nodeip;
        String errIp = "";
        String callBackIp = myConfig.getPropertiesConfig().getProperties().getProperty("callBackIp");

        //运行时传入ipAddress将会覆盖用例本身设置的nodeip
        String ipForTitle = request.getParameter("ipForTitle");

        String strCaseId = request.getParameter("caseId");
        String[] caseArr = strCaseId.split(",");
        for (int i = 0; i < caseArr.length; i++){
            //查询数据库获取用例脚本
            String where = "id=\"" + caseArr[i] + "\"";
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            SqlReturnModel sqlReturnModel = sqlDispatch.select("`data`,`caseName`,`describe`,`ip`", "testcase", where, "createtime");
            String data = sqlReturnModel.getSelectReturnData().get(0).get(0);
            data = data.replaceAll("<yxmsinglequotes>", "'");
            caseScript = JSON.parseArray(data);
            caseName = sqlReturnModel.getSelectReturnData().get(0).get(1);
            describe = sqlReturnModel.getSelectReturnData().get(0).get(2);
            if (Objects.equals(ipForTitle, "") || Objects.equals(ipForTitle, null)){
                //运行时传入ipAddress将会覆盖用例本身设置的nodeip
                nodeip = sqlReturnModel.getSelectReturnData().get(0).get(3);
            }else {
                nodeip = ipForTitle;
            }

            //检查脚本中是否有引用
            if (caseScript == null){
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("not find case!");
                return returnJson.toString();
            }
            //检查脚本中是否引用了其他脚本
            //检查actionDes对象中的elementQuote元素，查找出对应元素后
            for (int x = 0; x < caseScript.size(); x++){
                JSONObject oneStep = caseScript.getJSONObject(x);
                if (Objects.equals(oneStep.getString("isJump"), "1")){
                    continue;
                }
                if (Objects.equals(oneStep.getString("actionName"), "caseQuote")){
                    String name = oneStep.getJSONObject("actionDes").getString("caseQuote");
                    where = "caseName=\"" + name + "\"";
                    SqlReturnModel s1 = sqlDispatch.select("`data`", "testcase", where, "createtime");
                    String quoteData = s1.getSelectReturnData().get(0).get(0);
                    quoteData = quoteData.replaceAll("<yxmsinglequotes>", "'");
                    JSONArray quoteCase = JSON.parseArray(quoteData);
                    caseScript.remove(x);
                    caseScript.addAll(x, quoteCase);
                }
            }
            System.out.println(caseScript);

            //组装caseid caseName,并发送执行请求
            executeScript.put("caseId", caseArr[i]);
            executeScript.put("caseName", caseName);
            executeScript.put("data", caseScript);
            executeScript.put("callBackIp", callBackIp);

            //数据库中取出空ip 那就用系统配置的地址
            if (Objects.equals(nodeip, null) || Objects.equals(nodeip, "")){
                nodeip = myConfig.getPropertiesConfig().getProperties().getProperty("nodeForUItest");
            }
            //补上http协议标识
            if (!Objects.equals(nodeip.substring(0,4), "http")){
                nodeip = "http://" + nodeip;
            }

            Http http = new Http();
            HttpResponse hr = http.sendRequest("post", null, nodeip + "/efuli/testScheduler", executeScript.toJSONString());
            if (hr.getrCode() != 200){
                errIp = errIp + nodeip + "||";
            }

        }

        if (Objects.equals(errIp, "")){
            returnJson.setApiStatus(1);
            returnJson.setResult("全部用例装载完毕已经开始执行");
        }else {
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg("连接节点服务器" + errIp + "异常！！");
        }

        return returnJson.toString();
    }
}
